package com.ruoyi.zhoubao.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zhoubao.mapper.ReqZhoubaoMapper;
import com.ruoyi.zhoubao.domain.ReqZhoubao;
import com.ruoyi.zhoubao.service.IReqZhoubaoService;
import com.ruoyi.common.core.text.Convert;

/**
 * zhoubaoBaseService业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-16
 */
@Service
public class ReqZhoubaoServiceImpl implements IReqZhoubaoService 
{


    private static final Logger log = LoggerFactory.getLogger(ReqZhoubaoServiceImpl.class);

    @Autowired
    private ReqZhoubaoMapper reqZhoubaoMapper;

    /**
     * 查询zhoubaoBase
     * 
     * @param 需求单编号 zhoubaoBase主键
     * @return zhoubaoBase
     */
    @Override
    public ReqZhoubao selectReqZhoubaoBy需求单编号(String 需求单编号)
    {
        return reqZhoubaoMapper.selectReqZhoubaoBy需求单编号(需求单编号);
    }

    /**
     * 查询zhoubaoBase列表
     * 
     * @param reqZhoubao zhoubaoBase
     * @return zhoubaoBase
     */
    @Override
    public List<ReqZhoubao> selectReqZhoubaoList(ReqZhoubao reqZhoubao)
    {
        return reqZhoubaoMapper.selectReqZhoubaoList(reqZhoubao);
    }

    /**
     * 新增zhoubaoBase
     * 
     * @param reqZhoubao zhoubaoBase
     * @return 结果
     */
    @Override
    public int insertReqZhoubao(ReqZhoubao reqZhoubao)
    {
        return reqZhoubaoMapper.insertReqZhoubao(reqZhoubao);
    }

    /**
     * 修改zhoubaoBase
     * 
     * @param reqZhoubao zhoubaoBase
     * @return 结果
     */
    @Override
    public int updateReqZhoubao(ReqZhoubao reqZhoubao)
    {
        return reqZhoubaoMapper.updateReqZhoubao(reqZhoubao);
    }

    /**
     * 批量删除zhoubaoBase
     * 
     * @param 需求单编号s 需要删除的zhoubaoBase主键
     * @return 结果
     */
    @Override
    public int deleteReqZhoubaoBy需求单编号s(String 需求单编号s)
    {
        return reqZhoubaoMapper.deleteReqZhoubaoBy需求单编号s(Convert.toStrArray(需求单编号s));
    }

    /**
     * 删除zhoubaoBase信息
     * 
     * @param 需求单编号 zhoubaoBase主键
     * @return 结果
     */
    @Override
    public int deleteReqZhoubaoBy需求单编号(String 需求单编号)
    {
        return reqZhoubaoMapper.deleteReqZhoubaoBy需求单编号(需求单编号);
    }
    /**
     * 删除所有周报请求记录
     *
     * @return 删除的记录数
     */
    @Override
    public int  deleteReqZhoubaoByAll()
    {
        return reqZhoubaoMapper.deleteReqZhoubaoByAll();
    }

    /**
     * 导入周报数据
     *
     * @param zbList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importReqZhoubao(List<ReqZhoubao> zbList, Boolean isUpdateSupport, String operName)
    {
        long startTime = System.currentTimeMillis();
        if (StringUtils.isNull(zbList) || zbList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }

        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ReqZhoubao zbao : zbList)
        {
            try
            {

                zbao.setCreateBy(operName);
                reqZhoubaoMapper.insertReqZhoubao(zbao);
                    successNum++;
                    //successMsg.append("<br/>" + successNum + "、账号 " + zbao.get需求单编号() + " 导入成功");



            }
            catch (Exception e)
            {
                failureNum++;

                String msg = "<br/>" + failureNum +  " 导入失败：";
                //failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            long endTime = System.currentTimeMillis();
            long executionTime = (endTime - startTime) / 1000;
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：总共耗时："+executionTime+"（秒S）");
        }

        return successMsg.toString();
    }

    @Override
    public String  initZhoubao() {
        long startTime = System.currentTimeMillis();
        try {

            reqZhoubaoMapper.executeZhoubaoInit();
        }catch (Exception e) {
            log.error("加工数据周报请求记录失败", e);
            throw new ServiceException("加工数据周报请求记录失败: " + e.getMessage());
        }
        long endTime = System.currentTimeMillis();
        long executionTime = (endTime - startTime) / 1000;
        return "加工数据成功，总共耗时："+executionTime+"（秒S）";
    }
}
